﻿
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Wuqi.Webdiyer;
using Common;
using Mang24k99.Model.CLMN;
using Mang24k99.Business.CLMN;
using NHibernate.Expression;
namespace Mang24k99.Web.CLMN.KtClmnItems
{
    public partial class List : System.Web.UI.Page
    {
        protected string RootPath = VirtualPathUtility.ToAbsolute("~/");
        string _AspNetCostumHtmlName = "AspNetCostumHtmlName";
        string _layerName = "layer";
        string _parentNoName = "parentNo";
        string _IsRblPostBackName = "rblPostBack";

        public bool? IsRblPostBack
        {
            get
            {
                return ViewStateHelper.GetBool(this.ViewState, _IsRblPostBackName);
            }
            set
            {
                ViewStateHelper.SetViewState(this.ViewState, value, _IsRblPostBackName);
            }
        }
        public int? Layer
        {
            get
            {
                return ViewStateHelper.GetInt(this.ViewState, _layerName);
            }
            set
            {
                ViewStateHelper.SetViewState(this.ViewState, value, _layerName);
            }
        }


        public int? ParentNo
        {
            get
            {
                return ViewStateHelper.GetInt(this.ViewState, _parentNoName);
            }
            set
            {
                ViewStateHelper.SetViewState(this.ViewState, value, _parentNoName);
            }
        }
        public string AspNetCostumHtml
        {
            get
            {
                return ViewStateHelper.GetString(this.ViewState, _AspNetCostumHtmlName);
            }
            set
            {
                ViewStateHelper.SetViewState(this.ViewState, value, _AspNetCostumHtmlName);
            }
        }
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();

            }
        }
        /// <summary>
        /// Binds the data.(第一次加载时候绑定的数据)
        /// </summary>
        private void BindData()
        {
            this.AspNetCostumHtml = this.AspNetPager1.CustomInfoHTML;
            AspNetPager1_PageChanged(null, null);
        }
        /// <summary>
        /// Binds the ASP net pager.(绑定分页控件)
        /// </summary>
        private void BindAspNetPager(ICriterion[] where)
        {

            AspNetPager aspNetPager = this.AspNetPager1;
            int RecordCount = BKtClmnItems.Count();
            aspNetPager.RecordCount = RecordCount;
            aspNetPager.CustomInfoHTML = string.Format(this.AspNetCostumHtml, aspNetPager.PageCount);
            if (!IsPostBack)
            {
                int pageIndex;
                try
                {
                    pageIndex = int.Parse(this.Request.QueryString["pageIndex"]);
                    this.AspNetPager1.CurrentPageIndex = pageIndex;

                }
                catch (Exception)
                {
                }
            }
        }
        /// <summary>
        /// Binds the reapert.(绑定列表控件)
        /// </summary>
        private void BindReapert(ICriterion[] where, int findLayer, int parentNo)
        {
            int? conditionLayer = null;
            string rbl = rblList.SelectedValue;
            if (rbl != null && rbl != "-1")
            {
                if (parentNo == 0)
                {

                    switch (rbl)
                    {

                        case "2":
                            conditionLayer = 2;
                            parentNo = int.Parse(this.ddlList1.SelectedValue);
                            break;
                        case "3":
                            conditionLayer = 3;
                            parentNo = int.Parse(this.ddlList2.SelectedValue);
                            if (parentNo == 0)
                            {
                                conditionLayer = 1;
                                parentNo = int.Parse(this.ddlList1.SelectedValue);
                            }
                            break;

                        default:
                            break;
                    }
                }

            }

            if (conditionLayer == null)
            {
                conditionLayer = findLayer;
            }
            AspNetPager aspNetPager = this.AspNetPager1;
            Repeater repeater = this.Repeater1;
            int maxRecord = aspNetPager.PageSize;
            int startRecord = aspNetPager.StartRecordIndex - 1;
            object dataSource = BKtClmnItems.FindAllByLayerGeneral(conditionLayer, parentNo, findLayer);
            repeater.DataSource = dataSource;
            repeater.DataBind();
        }
        /// <summary>
        /// Handles the PageChanged event of the AspNetPager1 control.(列表改变时候)
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            int layer = 1;
            int parentNo = 0;
            if (!IsPostBack)
            {
                try
                {
                    ListItem li = this.rblList.Items.FindByValue(this.Request.QueryString["Rbl"]);
                    Layer = RequestHelper.GetInt(this, _layerName);
                    ParentNo = RequestHelper.GetInt(this, _parentNoName);

                    if (li != null)
                    {
                        this.rblList.SelectedIndex = -1;
                        li.Selected = true;

                        SetRblList(false);

                    }

                }
                catch
                {
                    //不做处理
                }

            }

            List<ICriterion> where = new List<ICriterion>();
            int partionIndex = GetRblSelectedValue();
            int No1 = 0;
            int No2 = 0;
            int No3 = 0;

            try
            {
                No1 = int.Parse(this.ddlList1.SelectedValue);
                No2 = int.Parse(this.ddlList2.SelectedValue);
                No3 = int.Parse(this.ddlList3.SelectedValue);

            }
            catch
            {

                //不做处理
            }


            switch (partionIndex)
            {
                case -1:
                    layer = 1;
                    parentNo = 0;
                    break;
                case 1:
                    layer = 2;
                    parentNo = No1;
                    break;
                case 2:
                    layer = 3;
                    parentNo = No2;
                    break;
                case 3:
                    layer = 4;
                    parentNo = No3;
                    break;
                default:
                    return;
                    break;
            }
            if (!IsPostBack)
            {
                if (Layer.HasValue)
                {
                    layer = Layer.Value;
                }
                if (ParentNo.HasValue)
                {
                    parentNo = ParentNo.Value;
                }
            }
            else
            {
                Layer = layer;
                ParentNo = parentNo;
            }



            BindAspNetPager(where.ToArray());
            if ((IsPostBack || layer == 1) && !(IsRblPostBack == null || IsRblPostBack.Value))
            {
                BindReapert(where.ToArray(), layer, parentNo);
            }
            IsRblPostBack = false;

        }

        /// <summary>
        /// Handles the PreRender event of the Repeater1 control.(列表程序之前)
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Repeater1_PreRender(object sender, EventArgs e)
        {
            //Repeater repeater = (Repeater)sender;
            //IEnumerable repeaterObject=repeater.Items;
            //集体加客户端属性
            //PageOp.SetRepeaterAttributes(repeaterObject, "btnDelete", "onclick", "return ConfirmDelete()");
        }
        /// <summary>
        /// Handles the ItemCommand event of the Repeater1 control.(列表控件字段按钮事件)
        /// </summary>
        /// <param name="source">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.RepeaterCommandEventArgs"/> instance containing the event data.</param>
        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            Button btnSource = e.CommandSource as Button;//事件按钮
            Button btnDelete = e.Item.FindControl("btnDelete") as Button;//删除按钮
            Button btnModify = e.Item.FindControl("btnModify") as Button;
            if (btnDelete.Enabled == false)//防客户端代码失效或者恶意提交
            {
                return;
            }
            if (btnModify.Enabled == false)
            {
                return;
            }
            object id;
            try
            {
                id = int.Parse(e.CommandArgument.ToString());
            }
            catch (Exception)
            {
                //转换失败忽略
                return;
            }
            if (btnSource.CommandName == btnDelete.CommandName)//若为删除按钮
            {
                Delete(id);
                AspNetPager1_PageChanged(null, null);
            }
            if (btnSource.CommandName == btnModify.CommandName)
            {
                GoUrl("modify" + "&id=" + id);
            }
        }
        protected void Delete(object id)
        {
            MKtClmnItems mKtClmnItems = BKtClmnItems.Find(id);
            if (mKtClmnItems != null)
            {
                BKtClmnItems.Delete(mKtClmnItems);
            }
        }
        protected void lnkbtnAdd_Click(object sender, EventArgs e)
        {
            GoUrl("add");
        }
        private void GoUrl(string op)
        {
            string url = GetUrl(op);
            Response.Redirect(url, true);
        }
        private string GetUrl(string op)
        {
            int pageIndex = this.AspNetPager1.CurrentPageIndex;
            string url = "Edit.aspx" + "?" + "pageIndex=" + pageIndex + "&" + "op=" + op + "&" + "rbl=" + this.rblList.SelectedValue + "&" + _layerName + "=" + Layer + "&" + _parentNoName + "=" + ParentNo;
            return url;
        }
        protected void lnkbtnList_Click(object sender, EventArgs e)
        {
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            AspNetPager1_PageChanged(null, null);

        }

        /// <summary>
        /// Sets the td list visible.(元素索引>partitionIndex为不显示)
        /// </summary>
        /// <param name="partitionIndex">Index of the partition.</param>
        /// <param name="maxIndex">Index of the max.</param>
        private void SetTdListVisible(int partitionIndex, int maxIndex)
        {

            Control thisControl = WebHelper.GetContentPlace(this);
            for (int i = 0; i <= partitionIndex; i++)
            {
                HtmlControl hc = thisControl.FindControl("tdDdlList" + i) as HtmlControl;
                if (hc != null)
                {
                    hc.Visible = true;
                }
            }
            for (int i = partitionIndex + 1; i <= maxIndex; i++)
            {
                HtmlControl hc = thisControl.FindControl("tdDdlList" + i) as HtmlControl;
                if (hc != null)
                {
                    hc.Visible = false;
                }

            }

        }


        /// <summary>
        /// Gets the RBL selected value.
        /// </summary>
        /// <returns></returns>
        private int GetRblSelectedValue()
        {
            int partionIndex = GetRblSelecteValue();
            SetTdListVisible(partionIndex, 3);
            return partionIndex;
        }

        /// <summary>
        /// Gets the RBL selecte value.
        /// </summary>
        /// <returns></returns>
        private int GetRblSelecteValue()
        {
            string selectedValue = rblList.SelectedValue;
            int partionIndex = -1;
            try
            {
                partionIndex = int.Parse(selectedValue);
            }
            catch
            {
            }
            return partionIndex;
        }

        protected void rblList_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetRblList(true);
        }

        private void SetRblList(bool isBindList)
        {
            IsRblPostBack = true;
            int index = GetRblSelectedValue();
            if (index == -1)
            {
                //选出一级列表,特殊对待
                IsRblPostBack = false;
            }
            else if (index == 1)//选出一级
            {

                BindDdlList(true, 1, 0, true);
            }
            else if (index == 2)//选出二级
            {
                BindDdlList(true, 1, 0, true);
                BindDdlList(false, 2, 0, true);
            }
            else if (index == 3)//选出三级
            {
                BindDdlList(true, 1, 0, true);
                BindDdlList(false, 2, 0, true);
                BindDdlList(false, 3, 0, true);
            }
            if (isBindList)
            {
                AspNetPager1_PageChanged(null, null);
            }

        }

        /// <summary>
        /// Binds the DDL list.(index的值为1,2,3)
        /// </summary>
        /// <param name="isHaveData">if set to <c>true</c> [is have data].</param>
        /// <param name="index">The index.</param>
        /// <param name="parentNo">The parent no.</param>
        private void BindDdlList(bool isHaveData, int index, int parentNo, bool isAutoPostBack)
        {

            Control thisControl = WebHelper.GetContentPlace(this);
            DropDownList ddl = thisControl.FindControl("ddlList" + index) as DropDownList;
            if (ddl == null)
            {
                //不做处理
                return;
            }

            IList<MKtClmnItems> dataSource;
            if (isHaveData)
            {
                dataSource = BKtClmnItems.FindAllByLayerDdlListGeneral(index, parentNo);
            }
            else
            {
                dataSource = new MKtClmnItems[0];
            }
            MKtClmnItems top = new MKtClmnItems();
            top.ColumnNO = 0;
            top.ColumnName = "所有";
            dataSource = ListHelper<MKtClmnItems>.GetList(dataSource, top);

            ddl.AutoPostBack = isAutoPostBack;
            ddl.DataSource = dataSource;
            ddl.DataTextField = "ColumnName";
            ddl.DataValueField = "ColumnNO";
            ddl.DataBind();
        }



        protected void ddlList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int parentNo = int.Parse(this.ddlList1.SelectedValue);
            if (parentNo == 0)
            {
                BindDdlList(false, 2, parentNo, true);
                BindDdlList(false, 3, parentNo, true);
            }
            else if (parentNo > 0)//有值
            {
                BindDdlList(true, 2, parentNo, true);
                BindDdlList(false, 3, parentNo, true);

            }
            else
            {
                //不做处理
            }
            if (GetRblSelecteValue() == 1)
            {
                AspNetPager1_PageChanged(null, null);
            }

        }

        protected void ddlList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int parentNo = int.Parse(this.ddlList2.SelectedValue);
            if (parentNo == 0)
            {

                BindDdlList(false, 3, parentNo, true);
            }
            else if (parentNo > 0)
            {
                BindDdlList(true, 3, parentNo, true);

            }
            else
            {
                //不做处理
            }
            if (GetRblSelecteValue() == 2)
            {
                AspNetPager1_PageChanged(null, null);
            }
        }

        protected void ddlList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            AspNetPager1_PageChanged(null, null);
        }
    }
}
